﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AviewLightSource;
using System.Windows;
using FinalTestMachine.View.Other;
using System.Threading;
using FinalTestMachine.Motion;
using FinalTestMachine.View;
using AM.Communication;
using AM.Tools;
using AM.Communication.Entity;
using IConnectDLL;

namespace FinalTestMachine.Connect
{
    internal class ConnectFactory
    {
        static ConnectFactory()
        {
            CreateOpt();

            WorkItemManagerHelper.workIterm.LoadedWorkItemEventHandler += () =>
            {
                Fun_OPTWrite();
            };

            CreateTestStaionConnect();

        }

        #region opt
        private static List<OPTHelper.OPTControlHelper> oPTControls = new List<OPTHelper.OPTControlHelper> (GlobalParams.OPTSum);
        private static List<OPTHelper.ChannelDataManager> OptChannelManager = new List<OPTHelper.ChannelDataManager> (GlobalParams.OPTSum);
        public static void Fun_OPTWrite()
        {
            for (int i = 0; i < GlobalParams.OPTSum; i++)
            {
                try
                {
                    if (GetOptChannel(i).IsFileExit)
                    {
                        GetOptChannel(i).Read();
                        GetOpt(i).SetIntensity(GetOptChannel(i).Channels);
                    }
                }
                catch (Exception ex)
                {
                    Motion.StaticInitial.Motion.WriteErrToUser("光源参数写入失败：" + ex.Message);
                }
            }
           
        }
        public static OPTHelper.OPTControlHelper GetOpt(int i)
        {
            if(i >= GlobalParams.OPTSum ||  i < 0)
            {
                throw new Exception($"获取OPT失败，当前索引{i}");
            }
            return oPTControls[i];
        }
        public static OPTHelper.ChannelDataManager GetOptChannel(int i)
        {
            if (i >= GlobalParams.OPTSum || i < 0)
            {
                throw new Exception($"获取OPT失败，当前索引{i}");
            }
            return OptChannelManager[i];
        }

        private static void CreateOpt()
        {
            for (int i = 0; i < GlobalParams.OPTSum; i++)
            {
                OptChannelManager.Add(new OPTHelper.ChannelDataManager(GlobalParams.OPTChannelSum, () => Motion.ParamPath_Motion.SelectedDirPath, $"opt{i+1}.ini"));
                oPTControls.Add(ConnectManagerHelper.CreateRegisterUI<OPTHelper.OPTControlHelper>($"optSetting{i+1}.ini", $"OPT{i+1}-{GlobalParams.OPTChannelSum}路", OptChannelManager[i]));
            }
        }
        #endregion


        #region 通讯对象实例

        /// <summary>
        /// 通讯对象管理器
        /// </summary>
        private static IConnectDLL.ConnectManagerHelper connectManager => ConnectBuilder.ConnectHelper;

        /// <summary>
        /// MES
        /// </summary>
        public static readonly IConnectDLL.IConnect tcpClientMes = ConnectBuilder.GetConnectInstance("MES通讯", CommunicationType.TCPClient, "MES");


        /// <summary>
        /// ZebraPrint
        /// </summary>
        //public static readonly IConnectDLL.IConnectT<string> tcpZebraPrint = ConnectBuilder.GetConnectInstance<string>("斑马打印机通讯", CommunicationType.TCPClient, "打印机");

        ///// <summary>
        ///// 准直仪
        ///// </summary>
        //public static readonly IConnectDLL.IConnectT<string> tcpConnect_Collimator = ConnectBuilder.GetConnectInstance<string>("准直仪通讯", CommunicationType.TCPClient);

        /// <summary>
        /// SN扫码枪
        /// </summary>
        public static readonly IConnectDLL.IConnect TCP_ScanCode = ConnectBuilder.GetConnectInstance("外部扫码枪", CommunicationType.TCPClient, "扫码枪");

        /// <summary>
        ///  治具扫码枪
        /// </summary>
        public static readonly IConnectDLL.IConnect TCP_ScanSocket = ConnectBuilder.GetConnectInstance("内部扫码枪", CommunicationType.TCPClient, "扫码枪");

        #region 测试工位
        /*///// <summary>
        ///// site1
        ///// </summary>
        //public static readonly IConnectDLL.IConnectT<UdpWpfDemo3.MTP> connectSite1 = ConnectBuilder.GetConnectInstance<UdpWpfDemo3.MTP>("工位一", CommunicationType.TCPServer, "测试工位");

        /// <summary>
        /// site2
        /// </summary>
        public static readonly IConnectDLL.IConnectT<UdpWpfDemo3.MTP> connectSite2 = ConnectBuilder.GetConnectInstance<UdpWpfDemo3.MTP>("工位二", CommunicationType.TCPServer, "测试工位");

        /// <summary>
        /// site3
        /// </summary>
        public static readonly IConnectDLL.IConnectT<UdpWpfDemo3.MTP> connectSite3 = ConnectBuilder.GetConnectInstance<UdpWpfDemo3.MTP>("工位三", CommunicationType.TCPServer, "测试工位");

        /// <summary>
        /// site4
        /// </summary>
        public static readonly IConnectDLL.IConnectT<UdpWpfDemo3.MTP> connectSite4 = ConnectBuilder.GetConnectInstance<UdpWpfDemo3.MTP>("工位四", CommunicationType.TCPServer, "测试工位");

        /// <summary>
        /// site4
        /// </summary>
        public static readonly IConnectDLL.IConnectT<UdpWpfDemo3.MTP> connectSite5 = ConnectBuilder.GetConnectInstance<UdpWpfDemo3.MTP>("工位四", CommunicationType.TCPServer, "测试工位");

        /// <summary>
        /// site4
        /// </summary>
        public static readonly IConnectDLL.IConnectT<UdpWpfDemo3.MTP> connectSite6 = ConnectBuilder.GetConnectInstance<UdpWpfDemo3.MTP>("工位四", CommunicationType.TCPServer, "测试工位");*/
        #endregion

        public static List<IConnectDLL.IConnectT<UdpWpfDemo3.MTP>> testStationConnections = new List<IConnectDLL.IConnectT<UdpWpfDemo3.MTP>>();
        public static IConnectDLL.IConnectT<UdpWpfDemo3.MTP> GetTestStationConnect(int index)
        {
            if(index < 0 || index > testStationConnections.Count)
            {
                return null;
            }
            return testStationConnections[index-1];
        }
        public static void CreateTestStaionConnect()
        {
            for (int i = 0; i < GlobalParams.WorkstationSum; i++) {
                testStationConnections.Add(ConnectBuilder.GetConnectInstance<UdpWpfDemo3.MTP>("工位" + (i + 1),CommunicationType.TCPServer,"测试工位"));
            }
        }

        /// <summary>
        /// 程控电源1
        /// </summary>
        public static readonly ProgrammablePowerEntity PowerSerialPort_1 = (ProgrammablePowerEntity)ConnectBuilder.GetConnectInstance("程控电源1", CommunicationType.ProgrammablePower, "程控电源");

        /// <summary>
        /// 程控电源2
        /// </summary>
        public static readonly ProgrammablePowerEntity PowerSerialPort_2 = (ProgrammablePowerEntity)ConnectBuilder.GetConnectInstance("程控电源2", CommunicationType.ProgrammablePower, "程控电源");


        /// <summary>
        /// 程控电源3
        /// </summary>
        public static readonly ProgrammablePowerEntity PowerSerialPort_3 = (ProgrammablePowerEntity)ConnectBuilder.GetConnectInstance("程控电源3", CommunicationType.ProgrammablePower, "程控电源");

        public static readonly IConnectDLL.IConnect AWBEnity = ConnectBuilder.GetConnectInstance("AWB积分球", CommunicationType.SerialPort, "AWB积分球");

        #region 调光器
        ///// <summary>
        /////  调光器1
        ///// </summary>
        //public static readonly SerialPortDemo3.ConnectSerialPortHelper SerialPort_LightModulator1 = (SerialPortDemo3.ConnectSerialPortHelper)ConnectBuilder.GetConnectInstance("调光器1", CommunicationType.SerialPort, "调光器");

        ///// <summary>
        /////  调光器2
        ///// </summary>
        //public static readonly SerialPortDemo3.ConnectSerialPortHelper SerialPort_LightModulator2 = (SerialPortDemo3.ConnectSerialPortHelper)ConnectBuilder.GetConnectInstance("调光器2", CommunicationType.SerialPort, "调光器");

        ///// <summary>
        /////  调光器3
        ///// </summary>
        //public static readonly SerialPortDemo3.ConnectSerialPortHelper SerialPort_LightModulator3 = (SerialPortDemo3.ConnectSerialPortHelper)ConnectBuilder.GetConnectInstance("调光器3", CommunicationType.SerialPort, "调光器");
        #endregion

        #endregion 通讯对象实例

        private static bool IsInitialed = false;

        public static void Initial()
        {
            if (IsInitialed)
                return;
            IsInitialed = true;

            oPTControls.ForEach(control => { connectManager.Add(control); });

            connectManager.Open();
            connectManager.StartReLink();

            ZebraHelper zebra = new ZebraHelper();
        }

        public static void Close()
        {
            connectManager.Close();
        }
    }
}